package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.CheckMobileOperator;
import com.qf.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 *
 * 号段补全策略
 * @author YDW
 * projectName:project2202_4
 * time:2022/6/20 22:28
 * description:
 */
@Service("phase")
@Slf4j
public class PhaseFilterServiceImpl implements FilterChainService {

    @Autowired
    CacheFeign cacheFeign;

    @Autowired
    PushMsgUtil pushMsgUtil;
    @Override
    public boolean strategy(StandardSubmit submit) {
        log.info("[策略模块 -- 号段补全策略] 策略开始执行！ submit={}",submit);


        String mobile=submit.getDestMobile();
        // 联通
        if(CheckMobileOperator.isChinaUnicomPhoneNum(mobile)){
            submit.setOperatorId(OperatorConstants.CHINA_UNICOM);
            submit.setGatewayID(OperatorConstants.CHINA_UNICOM);
        }//电信
        else if(CheckMobileOperator.isChinaTelecomPhoneNum(mobile)){
            submit.setOperatorId(OperatorConstants.CHINA_TELECOM);
            submit.setGatewayID(OperatorConstants.CHINA_TELECOM);
        }//移动
        else if(CheckMobileOperator.isChinaMobilePhoneNum(mobile)){
            submit.setOperatorId(OperatorConstants.CHINA_MOBILE);
            submit.setGatewayID(OperatorConstants.CHINA_MOBILE);
        }//未知
        else {
            submit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
            submit.setGatewayID(OperatorConstants.CHINA_UNKNOWN);
        }

        //获取手机号前7位
        String preMobile = mobile.substring(0, 7);
        //根据手机号码的号段，获取所属的省市id
        String instid = cacheFeign.get(CacheConstants.CACHE_PREFIX_PHASE + preMobile);
        if(!StringUtils.isEmpty(instid)){
            String[] arr = instid.split(",");
            //指定手机号所属省id
            submit.setProvinceId(Integer.parseInt(arr[0]));
            //手机号 市id
            submit.setCityId(Integer.parseInt(arr[1]));
        }else {
            //如果省市id没有获取,说明手机号非法
            log.info("[策略模块 -- 号段补全策略] 号段补全测试失败!!");
            //发送日志消息到mq队列中，后期会监听队列，并存储数据到ES中
            pushMsgUtil.sendLog(submit,"号段补全失败");

            //发送报告
            pushMsgUtil.pushReport(submit,"号段补全失败");


            return false;
        }


        Integer operatorId = submit.getOperatorId();
        Integer gatewayID = submit.getGatewayID();
        //发送短信的源号码
        //获取运营商集合
        Map channlmap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_SMS_CHANNEL+operatorId + ":" + gatewayID);

        //获取路由信息
        Map clientmap = cacheFeign.hMGet(CacheConstants.CACHE_PREFIX_ROUTER + submit.getClientID() + ":" + operatorId);

        if(!CollectionUtils.isEmpty(channlmap)&&!CollectionUtils.isEmpty(clientmap)){

            //客户端（千锋教育） 扩展码
            String extendnumber = String.valueOf(clientmap.get("extendnumber"));
            String spnumber = String.valueOf(channlmap.get("spnumber"));
            //运营商的 源号码
            submit.setSrcNumber(spnumber+extendnumber);


            String price = String.valueOf(clientmap.get("price"));
            //短信费用
            submit.setFee(Long.parseLong(price));
        }
        log.info("[策略模块 -- 号段补全策略] 号段补全策略执行成功!!");
        return true;
    }
}
